<template>
  <div class="app-draw-add">
	  <u-navbar :is-back="false" title="领货单填写"
	    title-size="30"
	    :background="{background:'#2B88FE' }"
	    title-color="#fff">
	    <div class="head_icon">
	      <u-icon @click="$api.handleBackTo"
	        name="arrow-left" color="#fff"
	        size="36">
	      </u-icon>
	      <u-icon @click="$api.handleHomeTo"
	        name="home" color="#fff" size="36">
	      </u-icon>
	    </div>
	  </u-navbar>
	  
    <div class="draw-content">
      <appProduct v-for="(v,i) in list" :key="i" :li="v">
        <view slot="cell" class="u-flex">
          <span> 还需领取：</span>
          <u-input border type="text" v-model="v.Count"></u-input>
        </view>
      </appProduct>
    </div>
    <navigator url="/pages/sale/draw/drawSelect" class="draw-add">
      <u-icon name="plus" color="#2979ff" size="28"></u-icon> 添加领货商品
    </navigator>
    <appProduct v-for="(v,i) in formdata" :key="i" :li="v">
		<view slot="cell" style="display:flex" v-if="v.AddType==0">
		  <span> 产品规格：</span>
		<picker @change="confirmPrice($event,v)" :range="v.PriceList" range-key="fUnit">
		  {{v.ProUnitName}}
		  <u-icon style="margin-left:10rpx" name="arrow-down-fill" color="#666" size="20">
		  </u-icon>
		</picker>
		</view>
      <view slot="cell" style="display:flex">
        <span> 领取数量：</span>
        <u-input border type="text" v-model="v.NeedCount"></u-input>
      </view>
    </appProduct>
    <div class="draw-mark">
      <span>备注（非必填）</span>
      <u-input type="textarea" v-model="info.Info" border></u-input>
    </div>
    <div class="footer">
      <u-button>取消</u-button>
      <u-button type="primary" @click="handleSave">提交</u-button>
    </div>
  </div>
</template>

<script>
import appProduct from '@/components/app-product'
export default {
  components: {
    appProduct
  },
  data() {
    return {
      info: {
        InOut: 2,
        iType: 2,
        CarId: 0,
        CarNo: '',
        Info: ''
      },
	 clineOnce:true,
      formdata: [],
      list: []
    }
  },
  methods: {
    handleSave() {
		if (!this.clineOnce) return false
		this.clineOnce = false
		// console.log('list2 ', this.list)
		//  console.log('formdata2 ', this.formdata)
        this.$axios('api/ProInAndOut/CarApplyProGet', 'post', { value: this.info }).then(ret => {
        this.list.forEach(x => (x.InAndOutId = ret.Value))
				let list=this.formdata.map(x=>({
					Count:x.NeedCount,
					InAndOutId:ret.Value,
					ProId:x.fId,
					Unit:x.ProUnitName,
					ProName:x.gname,
					Info:x.Info,
					fInOut:2,
					fType:2
				}))
				this.list=[].concat.apply(this.list,list)
				
				// console.log('list3 ', this.list)
        this.$axios('api/ProInAndOutList/AddListDetail', 'post', { value: this.list }).then(res => {
			if (!res.Success) {
			  uni.showToast({
			    title: '提交失败：'+res.Message,
			    icon: 'none',
			    duration: 2000
			  })
			 this.clineOnce = true
			  return false
			}
           this.$api.handleBack('添加成功')
		   getApp().globalData.addCusGoodLi=null
        })
      })
    },
	confirmPrice(e, item) {
		//console.log('onSelect --- ', item)
	    console.log('e --- ', e, item)
	   let info = item.PriceList[e.detail.value]
	 	//console.log('onSelect --- ', JSON.stringify(result))
	  item.ProUnitName = info.fUnit
 
	  this.$forceUpdate()
	}
  },
  onShow() {
    if (getApp().globalData.addCusGoodLi) {
      let orders = getApp().globalData.addCusGoodLi
      orders.forEach(x =>(
		  x.details = [
            { label: '车上库存', value: x.fCarCount },
			 { label: '仓库库存', value: x.fDefCount }
          ],
		   x.ProUnitName=x.fDefUnit,
		   	x.AddType=0
		   )
		
      )
	 this.formdata=[]
	  orders.forEach(x => {
	         x.NeedCount=0
	  		 let filtered = this.list.filter(item =>
	  		    item.ProId==x.fId
	  		  );
	  		  if(!filtered.length)
	  		  { 
				  this.formdata.push(x)
			  }
	    })
		
		// console.log('formdata1 ', this.formdata)
    }
  },
  created() {
    this.$axios('api/ProInAndOut/GetMyProInfo', 'post', { value: 0 }).then(res => {
      this.list = res.Value.map(x => ({
        ProId: x.ProId,
		id: x.ProId,
        ProName: x.ProName,
        gname: x.ProName,
        avatar: this.$api.OrderImg(x.ProId),
        NeedCount: x.NeedCount,
        Count: x.NeedCount,
		AddType: 1,
		 Unit: x.Unit,
		 fInOut:2,
		 fType:2,
		 ProUnitName:x.Unit,
        details: [
			{
			  label: '单位',
			  value: x.Unit
			},
          {
            label: '订单数量',
            value: x.BuyCount
          },
          {
            label: '车上库存',
            value: x.NowCount
          }
        ]
      }))
	  
	 // console.log('list1 ', this.list)
    })
  }
}
</script>

<style lang="scss" scoped>
.app-draw-add {
  padding-bottom: 70rpx;
  .draw-item {
    .title {
      font-size: 30rpx;
      color: #7f7f7f;
      margin-top: 30rpx;
      margin-left: 30rpx;
    }
  }
  .draw-add {
    color: #007aff;
    font-size: 30rpx;
    padding: 32rpx;
    text-align: center;
  }
  .draw-mark {
    display: flex;
    padding: 0 32rpx;
    u-input {
      flex: 1;
    }
  }
  .footer {
    margin-top: 32rpx;
    display: flex;
    justify-content: center;
    position: sticky;
    bottom: 2%;
    u-button,
    .u-btn {
      width: 40%;
    }
    u-button + u-button {
      margin-left: 30rpx;
    }
  }
}
</style>